Option Strict Off
Option Explicit On
Module Hotkeys
	'This routine handles all the hotkey controls. It is triggered by an interupt set up in the main frmAnimator form
	'In main routine start keyboard hook by using     hHook = SetWindowsHookEx(2, AddressOf KeyboardProc, App.hInstance, App.ThreadID) '2 = keyboard
	'End the keyboard hook on exiting using           UnhookWindowsHookEx hHook
	
	
	'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByRef lParam As Object) As Integer
    Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Short
    Public Delegate Function KeyboardHookDelegate(ByVal idHook As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
	Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
	Public hHook As Integer
	Public ExitHotkeyPressed As Boolean
	
	'These are public variables as the LoadSettings loads the shortcut ascii values to these variables
	Public Ascii07, Ascii05, Ascii03, Ascii01, Ascii02, Ascii04, Ascii06, Ascii08 As Integer
	Public Ascii15, Ascii13, Ascii11, Ascii09, Ascii10, Ascii12, Ascii14, Ascii16 As Integer
	Public Ascii17, Ascii18 As Integer
	
	'These are local variables to hold whether the keys have previously been released. This stops double triggers if the key is held down too long.
	Dim HK07, HK05, HK03, HK01, HK02, HK04, HK06, HK08 As Boolean 'The toggles that remember whether
	Dim HK15, HK13, HK11, HK09, HK10, HK12, HK14, HK16 As Boolean 'key has been released before
	Dim HK17, HK18 As Boolean 'The hotkey ascii values
	'Dim HFTargetHandle As Long                          'Handle to Helium Frog main window
	
	Public Function KeyboardProc(ByVal idHook As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
		
		If ActiveWindowIs <> "ButtonWindow" Then Exit Function 'Another window such as settings, utilities or editor window is open , so hotkeys are not active.....exit subroutine
		If GetFocus() = 0 Then Exit Function 'The handle to the currently active window is zero, ie another program has the user focus, so dont trigger any hotkeys
		
		'*******************************************************************************************************************************************
		'T - Time Lapse
		If GetKeyState(Ascii12) And &H1000 Then 'Key is pressed (&H1000)
			If HK12 = False Then 'Key was previously up
				HK12 = True 'Set the flag to show key is now down
				Call TimeLapseUtility() 'Call action
			End If
		Else
			HK12 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		If IsCapturing = True Then Exit Function 'The program is still capturing / saving, so dont action hotkeys
		If TimeLapseOnYesNo = True Then Exit Function 'If you are in timelapse mode, only the key above works all below are disabled
		
		'*******************************************************************************************************************************************
		'Space Bar - Capture
		If GetKeyState(Ascii01) And &H1000 Then 'Key is pressed (&H1000)
			If HK01 = False Then 'Key was previously up
				HK01 = True 'Set the flag to show key is now down
				Call Capture() 'Call action
			End If
		Else
			HK01 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Enter Key - Capture
		If GetKeyState(Ascii02) And &H1000 Then 'Key is pressed (&H1000)
			If HK02 = False Then 'Key was previously up
				HK02 = True 'Set the flag to show key is now down
				Call Capture() 'Call action
			End If
		Else
			HK02 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad 7 - To Start
		If GetKeyState(Ascii03) And &H1000 Then 'Key is pressed (&H1000)
			If HK03 = False Then 'Key was previously up
				HK03 = True 'Set the flag to show key is now down
				Call ToStart() 'Call action
			End If
		Else
			HK03 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad 4 - Back 1
		If GetKeyState(Ascii04) And &H1000 Then 'Key is pressed (&H1000)
			If HK04 = False Then 'Key was previously up
				HK04 = True 'Set the flag to show key is now down
				Call Back1() 'Call action
			End If
		Else
			HK04 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad 2 - Stop
		If GetKeyState(Ascii05) And &H1000 Then 'Key is pressed (&H1000)
			If HK05 = False Then 'Key was previously up
				HK05 = True 'Set the flag to show key is now down
				Call StopButt() 'Call action
			End If
		Else
			HK05 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad . - Play
		If GetKeyState(Ascii06) And &H1000 Then 'Key is pressed (&H1000)
			If HK06 = False Then 'Key was previously up
				HK06 = True 'Set the flag to show key is now down
				Call PlayButt() 'Call action
			End If
		Else
			HK06 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad 6 - Forward 1
		If GetKeyState(Ascii07) And &H1000 Then 'Key is pressed (&H1000)
			If HK07 = False Then 'Key was previously up
				HK07 = True 'Set the flag to show key is now down
				Call Forward1() 'Call action
			End If
		Else
			HK07 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad 9 - To End
		If GetKeyState(Ascii08) And &H1000 Then 'Key is pressed (&H1000)
			If HK08 = False Then 'Key was previously up
				HK08 = True 'Set the flag to show key is now down
				Call ToEnd() 'Call action
			End If
		Else
			HK08 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Keypad 0 - Loop
		If GetKeyState(Ascii09) And &H1000 Then 'Key is pressed (&H1000)
			If HK09 = False Then 'Key was previously up
				HK09 = True 'Set the flag to show key is now down
				Call LoopButt() 'Call action
			End If
		Else
			HK09 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'X - Exit
		If GetKeyState(Ascii10) And &H1000 Then 'Key is pressed (&H1000)
			If HK10 = False Then 'Key was previously up
				HK10 = True 'Set the flag to show key is now down
				ExitHotkeyPressed = True 'Set a flag that you want to end the program. You cant directly exit from here as when you unhook the keyboard in the exit                                                            'routine, the program will crash as this function is still running. Use a flag and exit from the main program loop
			End If
		Else
			HK10 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'/ - Toggle Windows
		If GetKeyState(Ascii11) And &H1000 Then 'Key is pressed (&H1000)
			If HK11 = False Then 'Key was previously up
				HK11 = True 'Set the flag to show key is now down
				Call Toggle() 'Call action
			End If
		Else
			HK11 = False 'Else the Key is not pressed
		End If
		
		'*******************************************************************************************************************************************
		'Keypad * - Onion Skinning
		If GetKeyState(Ascii13) And &H1000 Then 'Key is pressed (&H1000)
			If HK13 = False Then 'Key was previously up
				HK13 = True 'Set the flag to show key is now down
				Call OnionButt() 'Call action
			End If
		Else
			HK13 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'S - Settings
		If GetKeyState(Ascii14) And &H1000 Then 'Key is pressed (&H1000)
			If HK14 = False Then 'Key was previously up
				HK14 = True 'Set the flag to show key is now down
				Call Settings() 'Call action
			End If
		Else
			HK14 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'U - Utilities
		If GetKeyState(Ascii15) And &H1000 Then 'Key is pressed (&H1000)
			If HK15 = False Then 'Key was previously up
				HK15 = True 'Set the flag to show key is now down
				Call Utilities() 'Call action
			End If
		Else
			HK15 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'E - Editor
		If GetKeyState(Ascii16) And &H1000 Then 'Key is pressed (&H1000)
			If HK16 = False Then 'Key was previously up
				HK16 = True 'Set the flag to show key is now down
				Call Editor() 'Call action
			End If
		Else
			HK16 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'R - Rotoscope
		If GetKeyState(Ascii17) And &H1000 Then 'Key is pressed (&H1000)
			If HK17 = False Then 'Key was previously up
				HK17 = True 'Set the flag to show key is now down
				Call RotoScope() 'Call action
			End If
		Else
			HK17 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		'Del - Undo Button
		If GetKeyState(Ascii18) And &H1000 Then 'Key is pressed (&H1000)
			If HK18 = False Then 'Key was previously up
				HK18 = True 'Set the flag to show key is now down
				Call Undo() 'Call action
			End If
		Else
			HK18 = False 'Else the Key is not pressed
		End If
		'*******************************************************************************************************************************************
		KeyboardProc = CallNextHookEx(hHook, idHook, wParam, lParam)
		
	End Function
	
	Public Sub ReleaseKeyboard()
		UnhookWindowsHookEx(hHook) 'Release the keyboard hook
	End Sub
End Module